package cn.exrick.controller;

import cn.exrick.pojo.TbQuestionCat;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.front.QuestionCommentResult;
import cn.exrick.service.QuestionCatService;
import cn.exrick.service.QuestionService;
import cn.exrick.utils.ResultUtil;
import cn.exrick.utils.mybatis.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台 技术问答管理
 * @Description:
 */
@RestController
@Api(description= "技术问答管理")
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionCatService questionCatService;

    @RequestMapping(value = "/question/cat/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取问答分类列表")
    public DataTablesResult getList(){
        DataTablesResult result = new DataTablesResult();
        result.setData(questionCatService.getAll());
        return result;
    }

    @RequestMapping(value = "/question/cat/checkName",method = RequestMethod.GET)
    @ApiOperation(value = "判断问答分类是否已存在")
    public boolean checkName(String name){
        return questionCatService.getByName(name) == null;
    }

    @RequestMapping(value = "/question/cat/checkName/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "判断问答分类是否已存在")
    public boolean checkNameForEdit(@PathVariable long id, String name){
        return questionCatService.checkNameForEdit(id, name);
    }

    @RequestMapping(value = "/question/cat/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加问答分类")
    public Result<Object> addCat(@ModelAttribute TbQuestionCat tbQuestionCat){
        questionCatService.save(tbQuestionCat);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/question/cat/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新问答分类")
    public Result<Object> updateCat(@ModelAttribute TbQuestionCat tbQuestionCat){
        questionCatService.update(tbQuestionCat);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/question/cat/delete/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除问答分类")
    public Result<Object> deleteCat(@PathVariable long[] ids){
        for(long id : ids){
            int result = questionCatService.delete(id);
            if(result == 0){
                return new ResultUtil<Object>().setErrorMsg("id为"+id+"的分类被使用中，不能删除！");
            }
        }
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/question/list",method = RequestMethod.GET)
    @ApiOperation(value = "分页多条件搜索获取问答列表")
    public DataTablesResult getQuestionList(int draw, int start, int length, String searchKey, Long catId,
                                            String minDate, String maxDate,
                                            @RequestParam("search[value]") String search, @RequestParam("order[0][column]") int orderCol,
                                            @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"member_name", "created", "cat_name", "content", "image", "comment_count", "status"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "created";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if(!search.isEmpty()){
            searchKey=search;
        }
        DataTablesResult result = questionService.getQuestionPage(draw, start, length, searchKey, catId, minDate, maxDate, orderColumn, orderDir);
        return result;
    }

    @RequestMapping(value = "/question/CommentList/{questionId}",method = RequestMethod.GET)
    @ApiOperation(value = "分页多条件搜索获取问答评论列表")
    public Result<Object> getQuestionCommentList(@RequestParam(defaultValue = "0") int currentPage,
                                                 @RequestParam(defaultValue = "5") int size,
                                                 @PathVariable Long questionId){
        Page<QuestionCommentResult> questionResultPage = questionService.getQuestionCommentPageForApi(currentPage, size, questionId);
        return new ResultUtil<Object>().setData(questionResultPage);
    }

    @RequestMapping(value = "/question/updateStatus/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "显示/隐藏问答")
    public Result<Object> updateStatus(@PathVariable Long id){
        questionService.updateStatus(id);
        return new ResultUtil<Object>().setData(null);
    }

}
